package ar.edu.unlu.sistemas.p2p.android;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class InsertActivity extends AbsP2PActivity implements OnClickListener {
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.insert);
        Button btn = (Button)findViewById(R.id.button_insert);
        btn.setOnClickListener(this);
        RadioButton rbtn = (RadioButton)findViewById(R.id.radio_button_ins_doc);
        rbtn.setOnClickListener(this);
        rbtn = (RadioButton)findViewById(R.id.radio_button_ins_word);
        rbtn.setOnClickListener(this);
        rbtn = (RadioButton)findViewById(R.id.radio_button_ins_tf);
        rbtn.setOnClickListener(this);
        bindService(new Intent(InsertActivity.this,AppService.class), mConnection, BIND_AUTO_CREATE);
    }
    
    public void onClick(View v) {
    	EditText edt;
    	switch (v.getId()) {
			case R.id.button_insert:
				RadioGroup rg = (RadioGroup) this.findViewById(R.id.radio_group_ins_table);
				int sel = rg.getCheckedRadioButtonId();
				switch (sel) {
					case R.id.radio_button_ins_doc:
						edt = (EditText) this.findViewById(R.id.edit_text_1);
						this.appService.addDoc(edt.getText().toString());
						edt.setText(null);
						break;
					case R.id.radio_button_ins_word:
						edt = (EditText) this.findViewById(R.id.edit_text_1);
						this.appService.addWord(edt.getText().toString());
						edt.setText(null);
						break;
					case R.id.radio_button_ins_tf:
						edt = (EditText) this.findViewById(R.id.edit_text_1);
						String doc = edt.getText().toString();
						edt.setText(null);
						edt = (EditText) this.findViewById(R.id.edit_text_2);
						String word = edt.getText().toString();
						edt.setText(null);
						edt = (EditText) this.findViewById(R.id.edit_text_3);
						float value = Float.parseFloat(edt.getText().toString());
						edt.setText(null);
						this.appService.addTF(doc, word, value);
						break;
					default:
						break;
				}
				break;
			case R.id.radio_button_ins_doc:
				edt = (EditText) this.findViewById(R.id.edit_text_1);
				edt.setVisibility(View.VISIBLE);
				edt.setHint("Documento");
				edt = (EditText) this.findViewById(R.id.edit_text_2);
				edt.setVisibility(View.GONE);
				edt = (EditText) this.findViewById(R.id.edit_text_3);
				edt.setVisibility(View.GONE);
				break;
			case R.id.radio_button_ins_word:
				edt = (EditText) this.findViewById(R.id.edit_text_1);
				edt.setVisibility(View.VISIBLE);
				edt.setHint("Termino");
				edt = (EditText) this.findViewById(R.id.edit_text_2);
				edt.setVisibility(View.GONE);
				edt = (EditText) this.findViewById(R.id.edit_text_3);
				edt.setVisibility(View.GONE);
				break;
			case R.id.radio_button_ins_tf:
				edt = (EditText) this.findViewById(R.id.edit_text_1);
				edt.setVisibility(View.VISIBLE);
				edt.setHint("Documento");
				edt = (EditText) this.findViewById(R.id.edit_text_2);
				edt.setVisibility(View.VISIBLE);
				edt.setHint("Termino");
				edt = (EditText) this.findViewById(R.id.edit_text_3);
				edt.setVisibility(View.VISIBLE);
				edt.setHint("Valor");
				break;
			default:
				break;
    		}
    }
    
}
